import { _decorator, Component, Node, LabelComponent, Vec3, tween, UITransform, Size, CCObject, isValid } from 'cc';
import {PoolManager} from '../../framework/poolManager';
const { ccclass, property } = _decorator;

@ccclass('Tips')
export class Tips extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;
    @property(LabelComponent)
    public lbTips: LabelComponent = null!;
    
    private _oriPos: Vec3 = new Vec3();//初始位置
    private _targetPos: Vec3 = new Vec3(0, 150, 0);//目标位置

    start () {
        // Your initialization goes here.
    }

    public show (content: string, callback?: Function) {
        this.node.setPosition(this._oriPos);

        this.lbTips.string = content;
        let size: Size = this.lbTips.node.getComponent(UITransform)?.contentSize as Size;
        if (!isValid(size)) {//size不存在，自我销毁 
            PoolManager.instance.putNode(this.node);
            return;
        }
        this.node.getComponent(UITransform)?.setContentSize(size.width + 100 < 240 ? 240 : size.width + 100, size.height + 30);

        tween(this.node)
        .delay(0.8)
        .by(0.8, {position: this._targetPos})
        .call(()=>{
            callback && callback();
            PoolManager.instance.putNode(this.node);
        })
        .start();
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
